package com.ruoyi.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.energy.domain.*;
import com.ruoyi.energy.domain.vo.WXNyTransDetial;
import com.ruoyi.energy.mapper.NyTransCheckMapper;
import com.ruoyi.energy.mapper.NyTransLoadMapper;
import com.ruoyi.energy.mapper.NyTransMapper;
import com.ruoyi.energy.mapper.NyTransPartArrsMapper;
import com.ruoyi.energy.service.INyTransCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 变压器监测Service业务层处理
 *
 * @author cjie
 * @date 2021-10-11
 */
@Service
public class NyTransCheckServiceImpl implements INyTransCheckService {
    @Autowired
    private NyTransCheckMapper nyTransCheckMapper;

    @Autowired
    private NyTransMapper nyTransMapper;

    @Autowired
    private NyTransPartArrsMapper nyTransPartArrsMapper;

    @Autowired
    private NyTransLoadMapper nyTransLoadMapper;
    /**
     * 查询变压器监测
     *
     * @param id 变压器监测ID
     * @return 变压器监测
     */
    @Override
    public NyTransCheck selectNyTransCheckById(String id) {
        return nyTransCheckMapper.selectNyTransCheckById(id);
    }

    /**
     * 查询变压器监测列表
     *
     * @param nyTransCheck 变压器监测
     * @return 变压器监测
     */
    @Override
    public List<NyTransCheck> selectNyTransCheckList(NyTransCheck nyTransCheck) {
        return nyTransCheckMapper.selectNyTransCheckList(nyTransCheck);
    }

    /**
     * 新增变压器空开监测
     *
     * @param nyTransCheck 变压器空开监测
     * @return 结果
     */
    @Override
    public int insertNyTransCheck(NyTransCheck nyTransCheck) {
        nyTransCheck.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        nyTransCheck.setCreateTime(DateUtils.getNowDate());
        nyTransCheck.setCreateBy(SecurityUtils.getUsername());
        //更新空开表的当前值
//        if(nyTransCheck.getValue() >0){
//            NyTransPartArrs nyTransPartArr = new NyTransPartArrs();
//            nyTransPartArr.setAttrId(nyTransCheck.getSwiftId());
//            nyTransPartArr.setCurValue(nyTransCheck.getValue());
//            nyTransPartArrsMapper.updateNyTransPartArrs(nyTransPartArr);
//        }
        return nyTransCheckMapper.insertNyTransCheck(nyTransCheck);
    }

    /**
     * 修改变压器监测
     *
     * @param nyTransCheck 变压器监测
     * @return 结果
     */
    @Override
    public int updateNyTransCheck(NyTransCheck nyTransCheck) {
        return nyTransCheckMapper.updateNyTransCheck(nyTransCheck);
    }

    /**
     * 批量删除变压器监测
     *
     * @param ids 需要删除的变压器监测ID
     * @return 结果
     */
    @Override
    public int deleteNyTransCheckByIds(String[] ids) {
        return nyTransCheckMapper.deleteNyTransCheckByIds(ids);
    }

    /**
     * 删除变压器监测信息
     *
     * @param id 变压器监测ID
     * @return 结果
     */
    @Override
    public int deleteNyTransCheckById(String id) {
        return nyTransCheckMapper.deleteNyTransCheckById(id);
    }

    /*
    @Override
    public int saveWXNyTransCheck(WXNyTransDetial wxNyTransDetial) {
        int result =1;
        //微信箱变抄表记录保存
        NyTrans nyTrans = BeanUtil.toBean(wxNyTransDetial,NyTrans.class);
        //防止重复提交
        //保存 air 值
        List<NyTransPart> nyTransParts = wxNyTransDetial.getNyTransParts();
        List<NyTransPartArrs> nyTransPartArrSet = null;
        NyTransCheck nyTransCheck = new NyTransCheck();
        NyTransPartArrs nyTransPartArrs = null;
        for(NyTransPart nyTransPart:nyTransParts){
            nyTransPartArrSet = nyTransPart.getNyTransPartArrs();
            for(NyTransPartArrs nyTransPartArr:nyTransPartArrSet){
                nyTransPartArrs = nyTransPartArrsMapper.selectNyTransPartArrsById(nyTransPartArr.getAttrId());
                //保存记录值到omp_ny_trans_check
                if(nyTransPartArrs != null){
                    nyTransCheck.setSwiftId(nyTransPartArrs.getAttrId());
                    nyTransCheck.setEnterprise(nyTransPartArrs.getEn());
                    nyTransCheck.setTransId(nyTrans.getId());
                    nyTransCheck.setCheckBy(SecurityUtils.getUsername());
                    nyTransCheck.setValue(nyTransPartArr.getCurValue());
                    nyTransCheck.setCheckTime(DateUtils.getNowDate());
                    this.insertNyTransCheck(nyTransCheck);
                }
            }
        }

        //保存 Trans 当前总值
        //如果当前值不为空，否则累加计算总额，插入nyTransLoad
        if(true){
            nyTrans.setCurLoad(50L);
            nyTransMapper.updateNyTrans(nyTrans);
            NyTransLoad nyTransLoad = new NyTransLoad();
            nyTransLoad.setTransId(nyTrans.getId());
            nyTransLoad.setCheckTime(DateUtils.getNowDate());
            nyTransLoad.setValue(50L);
            nyTransLoad.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
            nyTransLoadMapper.insertNyTransLoad(nyTransLoad);
        }
        return result;
    }*/

    @Override
    public int saveWXNyTransCheck(WXNyTransDetial wxNyTransDetial) {
        int result =1;
        //微信箱变抄表记录保存
        NyTrans nyTrans = BeanUtil.toBean(wxNyTransDetial,NyTrans.class);
        //防止重复提交
        //保存 air 值
        List<NyTransPart> nyTransParts = wxNyTransDetial.getNyTransParts();
        List<NyTransPartArrs> nyTransPartArrSet = null;
        NyTransCheck nyTransCheck = new NyTransCheck();
        NyTransPartArrs nyTransPartArrs = null;
        for(NyTransPart nyTransPart:nyTransParts){
            nyTransPartArrSet = nyTransPart.getNyTransPartArrs();
            for(NyTransPartArrs nyTransPartArr:nyTransPartArrSet){
                nyTransPartArrs = nyTransPartArrsMapper.selectNyTransPartArrsById(nyTransPartArr.getAttrId());
                //保存记录值到omp_ny_trans_check
                if(nyTransPartArrs != null){
                    nyTransPartArrs.getNyEnteriseLoadList().stream().forEach(enLoad->{
                        nyTransCheck.setSwiftId(enLoad.getAttrId());
                        nyTransCheck.setEnterprise(enLoad.getId().toString());
                        //nyTransCheck.setTransId(nyTrans.getId());
                        nyTransCheck.setCheckBy(SecurityUtils.getUsername());
                        nyTransCheck.setValue(nyTransPartArr.getCurValue());
                        nyTransCheck.setCheckTime(DateUtils.getNowDate());
                        this.insertNyTransCheck(nyTransCheck);
                    });
                }
            }
        }
        long sum =0;
        if(wxNyTransDetial.getCurLoad() == null){
            for(NyTransPart part:wxNyTransDetial.getNyTransParts()){
                for(NyTransPartArrs partArrs:part.getNyTransPartArrs()){
                    for(NyEnteriseLoad load : partArrs.getNyEnteriseLoadList()){
                        sum += load.getCurValue();
                    }
                }
            }
        }else {
            sum = wxNyTransDetial.getCurLoad();
        }
        //保存 Trans 当前总值
        //如果当前值不为空，否则累加计算总额，插入nyTransLoad
        if(true){
            nyTrans.setCurLoad(sum);
            nyTransMapper.updateNyTrans(nyTrans);
            NyTransLoad nyTransLoad = new NyTransLoad();
            nyTransLoad.setTransId(nyTrans.getId());
            nyTransLoad.setCheckTime(DateUtils.getNowDate());
            nyTransLoad.setValue(sum);
            nyTransLoad.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
            nyTransLoadMapper.insertNyTransLoad(nyTransLoad);
        }
        return result;
    }

    @Override
    public WXNyTransDetial genWXCheckInfo(Long id) {
        //微信箱变抄表记录模板生成
        WXNyTransDetial result = null;
        result = nyTransMapper.selectWXCheckInfoByTransId(id);
        result.getNyTransParts().stream().forEach(part->{
            part.getNyTransPartArrs().stream().forEach(air->{
                air.setNyEnteriseLoadList(nyTransPartArrsMapper.selectNyEnteriseLoadByAttrId(air.getAttrId()));
            });
        });
        return result;
    }
    static Integer result =0;
    @Override
    public int batchInsertNyEnteriseLoad(List<NyEnteriseLoad> map) {
        if (StringUtils.isNull(map) || map.size() == 0) {
            throw new CustomException("批量抄录数据不能为空！");
        }
        NyTransCheck search = new NyTransCheck();
        NyTransCheck insert = new NyTransCheck();
        NyTransCheck update = new NyTransCheck();
        map.stream().forEach(item->{
            //nyTransCheck insert
            search.setEnterprise(item.getId().toString());
            search.setCheckTime(item.getCheckTime());
            List<NyTransCheck> nyTransChecks = nyTransCheckMapper.selectNyTransCheckList(search);
            if(nyTransChecks != null && nyTransChecks.size()>0){
                //update
                update.setId(nyTransChecks.get(0).getId());
                update.setValue(item.getCurValue());
                result=this.updateNyTransCheck(update);
            }else {
                //insert
                insert.setValue(item.getCurValue());
                insert.setEnterprise(item.getId().toString());
                insert.setCheckTime(item.getCheckTime());
                result = this.insertNyTransCheck(insert);
            }
        });
        return result;
    }

}
